package miaofusdk

/*
** ===============================================
** USER NAME: garlic(QQ:3173413)
** FILE NAME: resp_aliofflineshop.go
** DATE TIME: 2017-09-20 09:21:57
** ===============================================
 */

import (
	"encoding/json"
	"errors"
	"fmt"

	"gitee.com/garlic/tools"
)

/**
口碑门店详细数据..
暂时先用着..也准备更换掉.
实在是没时间
*/
type AliShopBean struct {
	Id                int    `json:"id,omitempty,string"`
	RequestId         string `json:"request_id,omitempty"`
	BizType           string `json:"biz_type,omitempty"`
	NotifyId          string `json:"notify_id,omitempty"`
	NotifyTime        string `json:"notify_time,omitempty"`
	NotifyType        string `json:"notify_type,omitempty"`
	ApplyId           string `json:"apply_id,omitempty"`
	AuditStatus       string `json:"audit_status,omitempty"`
	AuditDesc         string `json:"audit_desc,omitempty"`
	IsOnline          string `json:"is_online,omitempty"`
	IsShow            string `json:"is_show,omitempty"`
	Version           string `json:"version,omitempty"`
	SignType          string `json:"sign_type,omitempty"`
	Sign              string `json:"sign,omitempty"`
	ShopId            string `json:"shop_id,omitempty"`
	ResultCode        string `json:"result_code,omitempty"`
	ResultDesc        string `json:"result_desc,omitempty"`
	Qr_code           string `json:"qr_code,omitempty"`
	Processed_qr_code string `json:"processed_qr_code,omitempty"`
}

type RespAliShop struct {
	Response
	Data AliShopBean `json:"data,omitempty"`
}

func (s *SrvShopManager) UpdateAliShop(bean AliShopBean) (AliShopBean, error) {
	resp := RespAliShop{}
	http_request := s.GetPost(s.GetUrl() + "/ali_shop/update")
	m := tools.JsonToMap(bean)
	for k, _ := range m {
		value := fmt.Sprintf("%v", m[k])
		if value != "" {
			http_request.Param(k, value)
		}
	}
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) QueryAliShopByStoreCode(store_code string) (AliShopBean, error) {
	resp := RespAliShop{}
	http_request := s.GetPost(s.GetUrl() + "/ali_shop/query")
	http_request.Param("code", store_code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) QueryAliShopByShopId(shop_id string) (AliShopBean, error) {
	resp := RespAliShop{}
	http_request := s.GetPost(s.GetUrl() + "/ali_shop/query")
	http_request.Param("shop_id", shop_id)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}
